package UI;

import DB.userLib;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.sql.SQLException;

import java.util.Optional;


public class Register {
    private Stage stage;

    //注册逻辑
    public Register(Stage stage) {
        this.stage = stage;
        stage.setTitle("C1科一模拟考试系统注册");
        initImage();
    }

    public void initImage() {
        Image image = new Image("images\\img.png");
        stage.getIcons().add(image);

        //用户名、密码、姓名、年龄、性别、身份证号、身份证照片、手机号码、邮箱
        GridPane parent = new GridPane();
        parent.setAlignment(Pos.CENTER);
        parent.setHgap(10);
        parent.setVgap(10);
        parent.setPadding(new Insets(25, 25, 25, 25));
        Label labelFirst = new Label("欢迎使用C1科一模拟考试系统");
        labelFirst.setStyle("-fx-text-fill: #2f465e");
        labelFirst.setPadding(new Insets(0, 0, 10, 0));
        labelFirst.setTextFill(Color.BLUE);
        labelFirst.setFont(Font.font("Microsoft YaHei", 24));
        parent.add(labelFirst, 0, 0, 2, 1);

        TextField userName = new TextField();
        Label userNameLabel = new Label("用户名");
        parent.add(userNameLabel, 0, 1);
        parent.add(userName, 1, 1);
        PasswordField passWord = new PasswordField();
        Label passWordLabel = new Label("密码");
        parent.add(passWordLabel, 0, 2);
        parent.add(passWord, 1, 2);

        TextField name = new TextField();
        Label nameLabel = new Label("姓名");
        parent.add(name, 1, 3);
        parent.add(nameLabel, 0, 3);

        TextField gender = new TextField();
        gender.setPromptText("只能填男或女");
        Label genderLabel = new Label("性别");
        parent.add(gender, 1, 5);
        parent.add(genderLabel, 0, 5);

        TextField age = new TextField();
        Label ageLabel = new Label("年龄");
        parent.add(age, 1, 4);
        parent.add(ageLabel, 0, 4);

        TextField id = new TextField();
        Label idLabel = new Label("身份证号");
        parent.add(id, 1, 6);
        parent.add(idLabel, 0, 6);

        TextField phoneNumber = new TextField();
        Label phoneNumberLabel = new Label("手机号");
        parent.add(phoneNumber, 1, 7);
        parent.add(phoneNumberLabel, 0, 7);

        TextField email = new TextField();
        Label emailLabel = new Label("邮箱");
        parent.add(email, 1, 8);
        parent.add(emailLabel, 0, 8);

        Button logInButton = new Button("注册");
        logInButton.setPrefWidth(120);
        HBox hbBtn = new HBox(10);
        hbBtn.setAlignment(Pos.CENTER);
        hbBtn.getChildren().addAll(logInButton);
        parent.add(hbBtn, 1, 9);

        logInButton.setOnAction(e -> {
            //非空判断
            if (userName.getText().isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("用户名不能为空");
                alert.showAndWait();
            } else if (passWord.getText().isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("密码不能为空");
                alert.showAndWait();
            } else if (name.getText().isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("姓名不能为空");
                alert.showAndWait();
            } else if (gender.getText().isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("性别不能为空");
                alert.showAndWait();
            } else if (age.getText().isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("年龄也不能为空\n我们会为您保密的");
                alert.showAndWait();
            } else if (id.getText().isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("身份证号不能为空");
                alert.showAndWait();
            } else if (phoneNumber.getText().isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("手机号不能为空");
                alert.showAndWait();
            } else if (email.getText().isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("邮箱不能为空");
                alert.showAndWait();
            } else if (!(gender.getText().matches("[\u7537\u5973]"))) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("性别只能填男或女\n再检查检查吧？");
                alert.showAndWait();
            } else if (!(getRightId(id.getText()))) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("身份证号格式有误\n再检查检查吧？");
                alert.showAndWait();
            } else if (!(getRightPhone(phoneNumber.getText()))) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("手机号码格式有误\n再检查检查吧？");
                alert.showAndWait();
            } else if (!(getRightEmail(email.getText()))) {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("邮箱格式有误\n再检查检查吧？");
                alert.showAndWait();
            } else {
                try {
                    if (!(userLib.addUser(userName.getText(), passWord.getText(), name.getText(),
                            Integer.parseInt(age.getText()), gender.getText(), id.getText(), phoneNumber.getText(), email.getText(), "学习用户"))) {
                        Alert alert = new Alert(Alert.AlertType.WARNING);
                        alert.setContentText("用户名重复了，换一个试试？");
                        alert.showAndWait();
                    } else {
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.setContentText("您的账号已注册成功！！！\n登录试试");
                        Optional<ButtonType> result = alert.showAndWait();
                        if (result.get() == ButtonType.OK) {
                            // 返回登录界面
                            stage.close();
                            try {
                                new LogIn().start(new Stage());
                            } catch (Exception ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                    }
                } catch (SQLException ex) {
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setContentText("糟糕！！！出现了未知错误");
                    alert.showAndWait();
                    throw new RuntimeException(ex);
                }
            }
        });

        Scene scene = new Scene(parent, 450, 400);
        stage.setScene(scene);
        stage.centerOnScreen();
    }

    // 判断身份证号码格式
    public static boolean getRightId(String id) {
        String regex="[1-9]\\d{5}(?:18|19|20)\\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\\d|30|31)\\d{3}[\\dXx]";
        if(id.matches(regex)){
            return true;
        }else{
            return false;
        }
    }

    // 判断手机号码格式
    public static boolean getRightPhone(String phoneWard) {
        String regex="(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[189]))\\d{8}";
        if(phoneWard.matches(regex)){
            return true;
        }else{
            return false;
        }
    }

    //判断邮箱格式
    public static boolean getRightEmail(String email) {
        String regex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}";
        if (email.matches(regex)) {
            return true;
        } else {
            return false;
        }
    }

}